// powrspiral.txt. Use it to deactivate it. If player has enough Mechanics skill,
// it just turns off. Otherwise, it explodes.
// Cell 0 - Mechanics skill to deactivate it.
// Cell 1,2 - Stuff done flag which is set to 1 when it is off.
// Cell 3 - Num of d10 explosion does. Defaults to 12.
// Cell 4 - vulnerable to control key. If 1, having control key makes it just shut off.
// Cell 5 - cant turn it off without mechanics skill at least 2 less than difficulty

beginobjectscript;

variables;
short cur_tick;
short r1;
short exploding = 0;
short damage = 0;
short warned_once = 0;

body;

beginstate INIT_STATE;
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 
	if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) {
		if (get_flag(get_memory_cell(1),get_memory_cell(2)) == 0) {
			if (get_ran(1,0,100) < 40)
				create_text_bubble("Hummmm ...");
			
			//if (get_ran(1,0,100) < 40)
			//	run_sparkles_on_object(ME,17,5,1);							
			}
		}
	
	cur_tick = get_current_tick();
	
break;

beginstate 5;
	create_text_bubble("Spark! Hiss!");
	if (cur_tick != get_current_tick()) {
		cur_tick = get_current_tick();
		set_state(6);
		}
break;

beginstate 6;
	create_text_bubble("Smoke! Spark!");
	if (cur_tick != get_current_tick()) {
		cur_tick = get_current_tick();
		set_state(7);
		}
break;

beginstate 7;
	if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);

	play_sound(103);
	
	damage = get_memory_cell(3);
	if (damage == 0)
		damage = 12;
		
	r1 = get_ran(damage,1,10);
	damage_nearby(r1,4,1,2);
	r1 = get_ran(damage / 3,1,10);
	damage_nearby(r1,8,1,2);
	spray_missiles(58,8,8);

	kill_object(ME,0);
break;

beginstate USE_STATE;
	if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) {
		if (get_flag(get_memory_cell(1),get_memory_cell(2)) > 0) {
			print_str_color("Power Spiral: It's already deactivated.",2);
			end();
			}
		}
	if ((get_stat(21) < get_memory_cell(0) - 2) && (get_memory_cell(5) > 0)) {
		print_str_color("Power Spiral: This spiral is far too complicated. You can't figure",2);
		print_str_color("  out how to operate it.",2);
		end();
		}
	if (warned_once == 0) {
		warned_once = 1;
		print_str_color("Power Spiral: This spiral is providing power for some equipment.",2);
		print_str_color("  Use it again to try to deactivate it.",2);
		if (get_stat(21) < get_memory_cell(0)) 		
			print_str_color("  It looks dangerously complicated.",2);
		end();
		}
	if (exploding > 0) {
		print_str_color("Power Spiral: It's too late. It's going to explode.",2);
		end();
		}
	play_sound(166);
	if ((get_memory_cell(4) > 0) && (has_spec_item(2) > 0)) {
		if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
			set_flag(get_memory_cell(1),get_memory_cell(2),1);
		print_str_color("Power Spiral: You use your Control Key to shut off the power.",2);
		}
		else if (get_stat(21) < get_memory_cell(0)) {
			print_str("Power Spiral: Your skill is insufficient to control it. It's ");
			print_big_str("  going to explode! (Difficulty: ",get_memory_cell(0),")");
			pc_heard_sound(206); 
			exploding = 1;
			if (get_stat(21) < get_memory_cell(0) / 2)
				set_state(6);
				else set_state(6);
			}
			else {
				if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
					set_flag(get_memory_cell(1),get_memory_cell(2),1);
				create_text_bubble("Pfffft.");
				pc_heard_sound(239); 
				print_str_color("Power Spiral: You shut off the power.",2);
				}
break;
